//index.js
//获取应用实例
const App = getApp()
let AJAX = require("../../utils/util.js").AJAX
let timeTranfer = require("../../utils/util.js").timeTranfer
Page({  
   data: { 
      navH: '',
      typeList: [],
      hotList: [],
      articalList: [],
      searchValue: "",
      bannerList: [], 
      adList: [],
      noticeList: [],
      indicatorDots: false,
      autoplay: true,
      interval: 5000,
      duration: 1000,
      currentNum: 0,
      showBtns: false, // 电话弹出控制
      contact: false,
      couponDialog: false, // 新人礼弹出控制
      playList: [], // 一千种玩法
      coupon: [], // 礼包
      pic: '', // 总额
      couponID: "", // 一键领取参数 
      isDisabled: false, //是否可以点击
      uname: '', // 留言用户
      uphone: '', // 用户电话
      umsg: '', // 用户留言,
      total_pic:0
   },
   onLoad: function() {
      let vm =this
      this.setData({
         navH: App.globalData.navHeight
      })
      if (!wx.getStorageSync("ACCESSTOKEN")){
         getApp().userInfoReadyCallback=res=>{
            vm.requestType()
            vm.requestHot()

            vm.requestCarousel()
            vm.requestAd()
            vm.requestNotice()

            vm.requestPlay() // 一千种玩法
            vm.requestCouponList() // 优惠券
         }
         return
      }
      
      this.requestType()
      this.requestHot()
     
      this.requestCarousel()
      this.requestAd()
      this.requestNotice()
      
      this.requestPlay() // 一千种玩法
      this.requestCouponList() // 优惠券
   },
   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {
      if (App.globalData.ajaxIndex) {
         this.requestType()
         this.requestHot()
         // this.requestArtical()
         // this.requestCarousel()
         this.requestAd()
         App.globalData.ajaxIndex = false
      }

   },
   // 菜单接口
   requestType() {
      let vm = this
      AJAX("nav/nav-list").then(res => {

         vm.setData({
            typeList: res.data
         })
      })
   },
   // 热门活动
   requestHot() {
      let vm = this
      AJAX("product/hot-list").then(res => {
         let _data = res.data 
        
         _data.forEach((item,val)=>{
            item.pre_sales = parseInt(item.pre_sales) + parseInt(item.total_sales)            
         })
         
         if(_data.length>5){
           _data = _data.splice(0,5)
         }
         vm.setData({
            hotList: _data
         })
      })
   },
   //  行知大讲堂
   requestArtical() {
      let vm = this
      AJAX("articles").then(res => {
         console.log(res.data)
         let _data = []
         if (res.data && res.data.items) {
            res.data.items.forEach(function(item, index) {
               item.published_at = timeTranfer(item.published_at)
               console.log(item.published_at)
               _data.push(item)
            })
         }
         vm.setData({
            articalList: _data
         })
      })
   },
   requestCarousel() {
      let vm = this
      AJAX("carousel/info", "GET", {
         key: 'banner'
      }).then(res => { 
         
         vm.setData({
            bannerList: res.data.item,

         })
      })
   },
   // 站内信
   requestNotice() {
      let vm = this
      AJAX("message/get-message").then(res => {
         vm.setData({
            noticeList: res.data
         })
      })
   },
   // 评测 特惠 视频库
   requestAd() {
      let vm = this
      AJAX("carousel/info", "GET", {
         key: 'index_middle'
      }).then(res => {
         vm.setData({
            adList: res.data.item
         })

      })
   },
   // 一千零一种玩法	
   requestPlay() {
      let vm = this
      AJAX('idea/first').then(res => {

         //获取成功存储id 进行详情页的使用
         wx.setStorageSync('DETAILID', res.data.id)
         vm.setData({
            playList: res.data
         })
         // 获取id
         // console.log(wx.getStorageSync('DETAILID'))
      })
   },
   // 更多优惠券查看
   jump() {
     
      wx.navigateTo({
         url: '/pages/mine/coupon/coupon',
      })
   },
   //导航跳转
 
   makePhone(e) {
      let _phone = e.currentTarget.dataset.phone
      if (!_phone) {
         return
      }
      wx.makePhoneCall({
         phoneNumber: _phone,
      })
   },
   // 新人大礼包
   requestCouponList() {
      let vm = this
      AJAX('coupon/top-list').then(res => {
         // console.log(res.data)
         // 定义showDialog
         let showDialog = false
         // 判断 total_count是否大于0如果是现实新人大礼包，否则不显示
         if (res.data.total_count > 0) {
            showDialog = true
         }
         let _data = [],
            arr = []
         if (res.data && res.data.list) {
            res.data.list.forEach(function(item, index) {
               arr.push(item.coupon_id)
               item.start_time = timeTranfer(item.start_time, 1)
               item.end_time = timeTranfer(item.end_time, 1)
               item.price = parseInt(item.price)
               item.floor_price = parseInt(item.floor_price)
               _data.push(item)
              
            })
         }

         vm.setData({
            coupon: _data,
            pic: res.data.total_price,
            couponDialog: showDialog,
            couponID: arr,

         })

      })
   },




   /**
    * 
    * 优惠券列表
    */
   requestCoupon() {
      let vm = this
      AJAX('coupon/list', 'GET', {
         position: 1
      }).then(res => {
         let showDialog = false
         if (res.data.length > 0) {
            showDialog = true
         }
         vm.setData({
            couponData: res.data,
            couponDialog: showDialog
         })
      })
   },
   /**留言 */
   confirmMsg() {
      let vm = this
      AJAX('applicant/submit-applicant', 'POST', {
         applicant_type: 'parents',
         applicant_name: this.data.uname,
         applicant_phone: this.data.uphone,
         applicant_message: this.data.umsg
      }).then(res => {
         if (res.status === 'succeed') {
            wx.showToast({
               title: '留言成功，我们将尽快与您联系',
               icon: "none"
            })
            this.setData({
               contact: false
            })

         }
      })

   },
   inputInfo(e) {
      this.setData({
         uname: e.detail.value
      })
      //console.log(e.detail.value)
   },
   inputInfoPhone(e) {
      this.setData({
         uphone: e.detail.value
      })
      //console.log(e.detail.value)
   },
   inputInfoMsg(e) {
      this.setData({
         umsg: e.detail.value
      })
      //console.log(e.detail.value)
   },
   navBack() {
      wx.navigateTo({
         url: '/pages/notice/notice',
      })
   },
   toCompany() {
      wx.navigateTo({
         url: '/pages/company/comintro/comintro',
      })
      // wx.navigateTo({
      //   url: '/pages/company/introduction/introduction',
      // })
   },
   /**
    * receive
    * 一键领取 
    */
   receive() {
      //console.log("点击")
      console.log(this.data.couponID)
      let strCouponID = this.data.couponID.join(',')
      let vm = this
      AJAX('coupon-member/receive-coupon', 'POST', {
         coupon_id: strCouponID
      }).then(res => {
         wx.showToast({
            title: '领取成功',
            icon: "none"
         })
         vm.setData({
            isDisabled: true
         })
      })
   },

   routerToList(e) {
      let _id = e.currentTarget.dataset.id
      let _type = e.currentTarget.dataset.type
      if (_type == 2) {
         wx.navigateTo({
            url: '/pages/product/productlist/productlist?id=' + _id,
         })
         return
      }
      wx.navigateTo({
         url: '/pages/product/curselist/curselist?id=' + _id,
      })

   },
   toProductList(e) {
      let _type = e.currentTarget.dataset.type
      wx.navigateTo({
         url: '/pages/product/productlist/productlist?type=hot'
      })
   },
   toProduct(e) {
      let _id = e.currentTarget.dataset.id
      wx.navigateTo({
         url: '/pages/product/productinfo/productinfo?id=' + _id,
      })
   },
   tocurseList() {
      wx.navigateTo({
         url: '/pages/companycurse/curselist/curselist',
      })
   },
   toInfo(e) {
      let _id = e.currentTarget.dataset.id
      wx.navigateTo({
         url: '/pages/companycurse/companyinfo/companyinfo?id=' + _id,
      })
   },

   toSearchList(e) {
      let _val = e.detail.value
      wx.navigateTo({
         url: '/pages/product/search/search?val=' + _val,
      })
   },
   showBtns() {
      // console.log('1111')
      this.setData({
         contact: true
      })
   },
   closeContactDialog() {
      this.setData({
         contact: false
      })
   },
   closeCouponDialog() {
      this.setData({
         couponDialog: false,
         contact: false
      })
   },
   scEvent() {
      return false
   }
})